﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MathGraphics;

namespace MathGraphicsUnitTest
{
    /// <summary>
    /// 测试矩形与圆是否相交的算法
    /// </summary>
    public partial class frmJudgeIntersect : Form
    {
        private Circle _circle = new Circle(new PointF(100, 100), 10);
        private Rectangle _rec = new Rectangle(100, 100, 100, 100);

        public frmJudgeIntersect()
        {
            InitializeComponent();
        }

        private void pbGraphics_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.Green, _rec);
            e.Graphics.DrawEllipse(Pens.Red, new RectangleF(_circle.X - _circle.Radius, _circle.Y - _circle.Radius, _circle.Radius * 2, _circle.Radius * 2));
            if (MathGraphics.GraphicsMethod.RectangeAndCircleIntersect(_rec , _circle))
            {
                e.Graphics.DrawString("相交", Font, Brushes.Black, Point.Empty);
            }
            else
            {
                e.Graphics.DrawString("不相交", Font, Brushes.Black, Point.Empty);
            }
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            _circle.Y--;
            pbGraphics.Invalidate();
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            _circle.X ++;
            pbGraphics.Invalidate();
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            _circle.Y++;
            pbGraphics.Invalidate();
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            _circle.X--;
            pbGraphics.Invalidate();
        }

        private int _moveDir = -1;

        private void btnUp_MouseDown(object sender, MouseEventArgs e)
        {
            _moveDir = 0;
            timerMove.Start();
        }

        private void btnRight_MouseDown(object sender, MouseEventArgs e)
        {
            _moveDir = 1;
            timerMove.Start();
        }

        private void btnDown_MouseDown(object sender, MouseEventArgs e)
        {
            _moveDir = 2;
            timerMove.Start();
        }

        private void btnLeft_MouseDown(object sender, MouseEventArgs e)
        {
            _moveDir = 3;
            timerMove.Start();
        }

        private void btnUp_MouseUp(object sender, MouseEventArgs e)
        {
            timerMove.Stop();
        }

        private void timerMove_Tick(object sender, EventArgs e)
        {
            switch (_moveDir)
            {
                case 0:
                    _circle.Y-=2;
                    break;
                case 1:
                    _circle.X+=2;
                    break;
                case 2:
                    _circle.Y+=2;
                    break;
                case 3:
                    _circle.X-=2;
                    break;
                default:
                    break;
            }
            pbGraphics.Invalidate();
        }
    }
}
